<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Column;
use App\Topic;
use App\User;
use App\Question;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class QuestionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $flag = false;
        $clm = [];
        //筛选条件 : 问题名称, 作者, 栏目, 问题状态, 每页条数
        $columns = Column::get();//获取栏目清单
        foreach ($columns as $k => $v) {
            $clm[$v->id] = $v->title;
        }
        // dd($clm);
        // $topics = Topic::get();//获取话题清单
        $questions = Question::where(function($query) use ($request){
            if(!empty($request->title)){
                $query->where('title','like','%'.$request->title.'%');
            }
            if(!empty($request->status)){
                $query->where('status',$request->status);
            }
            if(!empty($request->column)){
                $query->where('column_id',$request->column);
            }
            if(!empty($request->author)){
                $query->where('user_id',$request->author);
            }
            $query->where('status', '<', 3);
        })->paginate($request->input('num',10));
        return view('admin.question.index',[
                'title'    => '问题列表',
                'questions'   => $questions,
                'request'  => $request,
                'columns'  => $clm,
                'flag'     => $flag
            ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function showDelQuestion(Request $request)
    {
        $flag = true;
        $clm = [];
        //筛选条件 : 问题名称, 作者, 栏目, 问题状态, 每页条数
        $columns = Column::get();//获取栏目清单
        foreach ($columns as $k => $v) {
            $clm[$v->id] = $v->title;
        }
        // dd($clm);
        // $topics = Topic::get();//获取话题清单
        $questions = Question::where(function($query) use ($request){
            if(!empty($request->title)){
                $query->where('title','like','%'.$request->title.'%');
            }
            if(!empty($request->status)){
                $query->where('status',$request->status);
            }
            if(!empty($request->column)){
                $query->where('column_id',$request->column);
            }
            if(!empty($request->author)){
                $query->where('user_id',$request->author);
            }
            $query->where('status', '=', 3);
        })->paginate($request->input('num',10));
        return view('admin.question.index',[
                'title'    => '问题列表',
                'questions'   => $questions,
                'request'  => $request,
                'columns'  => $clm,
                'flag'     => $flag
            ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $question = question::find($id);
        $user = User::find($question->user_id)->username;
        $column = Column::find($question->column_id)->title;//获取栏目

        return view('admin.question.show',[
                'title'=>'话题详情',
                'question'=>$question,
                'username'=>$user,
                'column'=>$column
            ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $question = Question::find($id);
        $question->status = 2;
        if($question->save()){
            return redirect('/admin/question');
        }else{
            return back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $question = Question::find($id);
        $question->status = 3;
        if($question->save()){
            return redirect('/admin/question');
        }else{
            return back();
        }
    }
}
